<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li :class="{fade: currentIndex === index}" v-for="(item, index) in sliders" :key="item.id" class="carousel-item">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <a @click="toggle(true)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="toggle(false)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span :class="{active: currentIndex === i - 1}" v-for="i in sliders.length" :key="i"></span>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, watch } from 'vue'

export default {
  name: 'XtxCarousel',
  props: {
    sliders: {
      type: Array,
      default: () => []
    },
    duration: {
      type: Number,
      default: 1
    },
    autoplay: {
      type: Boolean,
      default: true
    }
  },
  setup (props) {
    // 默认显示图片的索引
    const currentIndex = ref(0)
    // 自动播放
    let timer = null
    const autoPlayFn = () => {
      timer = setInterval(() => {
        currentIndex.value += 1
        if (currentIndex.value > props.sliders.length - 1) {
          // 控制右边界
          currentIndex.value = 0
        } else if (currentIndex.value < 0) {
          // 控制左边界
          currentIndex.value = props.sliders.length - 1
        }
      }, props.duration * 1000)
    }
    // 监听如果轮播图数据有变化时触发定时器函数
    watch(() => props.sliders, () => {
      if (props.autoplay && props.sliders.length > 1) {
        // 从第一张开始播
        currentIndex.value = 0
        autoPlayFn()
      }
    })
    // 组件销毁清除定时器
    onMounted(() => {
      if (timer) clearInterval(timer)
    })

    // 启动
    const start = () => {
      // 启动前先销毁之前的定时器
      if (timer) clearInterval(timer)
      // 再重新启动一个新的定时任务
      if (props.autoplay && props.sliders.length > 1) {
        autoPlayFn()
      }
    }

    // 暂停
    const stop = () => {
      if (timer) clearInterval(timer)
    }

    // 切换手动控制
    const toggle = (type) => {
      if (type) {
        // 点击左侧按钮
        if (currentIndex.value <= 0) {
          currentIndex.value = props.sliders.length - 1
        } else {
          currentIndex.value -= 1
        }
      } else {
        // 点击右侧按钮
        if (currentIndex.value >= props.sliders.length - 1) {
          currentIndex.value = 0
        } else {
          currentIndex.value += 1
        }
      }
    }

    return { currentIndex, start, stop, toggle }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel{
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel{
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
